package com.nike.amoeba.ui.frg

import android.os.Bundle
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ConvertUtils
import com.blankj.utilcode.util.StringUtils
import com.nike.amoeba.R
import com.nike.amoeba.adapter.AssetAdapter
import com.nike.amoeba.api.UIHelper
import com.nike.amoeba.base.BaseFragment
import com.nike.amoeba.bean.db.AssetBean
import com.nike.amoeba.databinding.BNotTitleRecyclerBinding
import com.nike.amoeba.ext.INVENTORY_NOT
import com.nike.amoeba.ext.INVENTORY_STOCK
import com.nike.amoeba.ext.init
import com.nike.amoeba.ext.setNbOnItemClickListener
import com.nike.amoeba.viewmodel.AssetModel
import com.nike.amoeba.weight.recyclerview.SpaceItemDecoration
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import me.hgj.jetpackmvvm.ext.nav

/**
 * @Author nike
 * @Date 2023/7/27 16:18
 * @Description 在库
 */
class AssetInStockFrg: BaseFragment<AssetModel, BNotTitleRecyclerBinding>(){

    private val assetModel: AssetModel by activityViewModels()

    val adapter: AssetAdapter by lazy { AssetAdapter(arrayListOf()) }

    var searchText: String? = null

    var fmIsVisible = false

    override fun initView(savedInstanceState: Bundle?) {
        arguments?.let {
            mViewModel.orderId.set(it.getString("orderId"))
            mViewModel.code.set(it.getString("code"))
        }
        mDatabind.swipeRefresh.isEnabled = false
        //初始化recyclerView
        mDatabind.recyclerView.init(LinearLayoutManager(context), adapter).let {
            it.addItemDecoration(SpaceItemDecoration(ConvertUtils.dp2px(20f), ConvertUtils.dp2px(20f), true))
        }
        adapter.run {
            setNbOnItemClickListener{adapter, view, position ->
                val bean = mFilterList[position]
                UIHelper.startAssetDetailsFrg(nav(), bean)
//                LogUtils.e(bean.LabelTag)
            }
            setSearchCallback(object :AssetAdapter.SearchCallback{
                override fun onSearchResults(filteredData: ArrayList<AssetBean>) {
//                    assetModel.assetTitle.value = getString(R.string.found) + "(" + filteredData.size + ")"
                }
            })
        }

        mViewModel.onRequest(mViewModel.orderId.get(), INVENTORY_STOCK, mViewModel.code.get())

        lifecycle.addObserver(object : DefaultLifecycleObserver {
            override fun onResume(owner: LifecycleOwner) {
                super.onResume(owner)
                fmIsVisible = true
                setToolBarTitle(getString(R.string.found) + "(" + adapter.mFilterList.size + ")")
            }

            override fun onPause(owner: LifecycleOwner) {
                super.onPause(owner)
                fmIsVisible = false
            }
        })
    }


    override fun createObserver() {
        super.createObserver()
        mViewModel.listBean.observe(viewLifecycleOwner, Observer {
            adapter.setList(it)
            adapter!!.appendList(it)
        })
        //搜索
        assetModel.assetSerch.observe(viewLifecycleOwner, {
            searchText = it
            adapter!!.filter.filter(searchText)
            if (viewLifecycleOwner.lifecycle.currentState == Lifecycle.State.RESUMED) {
                GlobalScope.launch(Dispatchers.Main) {
                    delay(300)
                    setToolBarTitle(getString(R.string.full_list) + "(" + adapter.mFilterList.size + ")")
                }
            }
        })
        //识别 / 更新 RFID
        assetModel.epcUploadData.observe(viewLifecycleOwner, {
            if (it == null || it.InventoryStatus != INVENTORY_STOCK)return@observe
            adapter.data.filterIndexed { index, bean ->
                val shouldBeIncluded = (!StringUtils.isEmpty(bean.LabelTag) && !bean.LabelTag.equals("null", ignoreCase = true) && bean.LabelTag.equals(it.LabelTag, ignoreCase = true))
                        || (!StringUtils.isEmpty(bean.AssetNo) && !bean.AssetNo.equals("null", ignoreCase = true) && bean.AssetNo.equals(it.AssetNo, ignoreCase = true))
                if (shouldBeIncluded) {
                    return@observe
                }
                shouldBeIncluded
            }
            adapter.addData(0, it)
            //过滤重复在库的操作 /增加在库
            /*if (it.InventoryStatus == INVENTORY_STOCK){
                adapter.data.filterIndexed { index, bean ->
                    val shouldBeIncluded = (!StringUtils.isEmpty(bean.LabelTag) && bean.LabelTag.equals(it.LabelTag)) || bean.AssetNo.equals(it.AssetNo)
                    if (shouldBeIncluded) {
                        return@observe
                    }
                    shouldBeIncluded
                }
                adapter.addData(0, it)
            }else{
                //在库变不在库
                val indexList = mutableListOf<Int>()
                adapter.data.filterIndexed { index, bean ->
                    val shouldBeIncluded = (!StringUtils.isEmpty(bean.LabelTag) && bean.LabelTag.equals(it.LabelTag)) || bean.AssetNo.equals(it.AssetNo)
                    // 将满足条件的索引添加到 indexList
                    if (shouldBeIncluded) {
                        indexList.add(index)
                    }
                    shouldBeIncluded
                }
                //更新item状态
                indexList.forEachIndexed() { index, i ->
                    adapter.removeAt(i)
                }
            }*/
//            mDatabind.recyclerView.scrollToPosition(0)
            //更新搜索页面item状态
            if (!StringUtils.isEmpty(searchText)){
                adapter!!.filter.filter(searchText)
            }
            if (fmIsVisible)setToolBarTitle(getString(R.string.found) + "(" + adapter.mFilterList.size + ")")

        })
    }

}